<?php


namespace app\admin\command;

use app\admin\model\ReservePays;
use app\admin\model\Store;
use app\admin\model\Transaction;
use app\admin\model\user\Card;
use app\admin\model\Reserve;
use app\admin\model\Room;
use app\common\library\Yunding;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;

class TransReserve extends Command
{

    protected function configure()
    {
        $this->setName('trans_reserve')
            ->setDescription('把reserve和reserve_pays表里的数据迁移一下');
    }

    protected function execute(Input $input, Output $output)
    {
        @ini_set("memory_limit", '-1');
        $stime = microtime(true);

        // $sql = 'select * from `dancestudionew_f`.`fa_reserve` where 1';

        // $kk = Db::query($sql);
        
        // $count = 0;
        // foreach ($kk as $item) {
        //     Reserve::create([
        //         'id' => $item['id'],
        //         'trade_no' => Carbon::parse($item['starttime'])->format('YmdHis') . str_pad(rand(0, 999999), 6, '0', STR_PAD_LEFT),
        //         'user_id' => $item['user_id'],
        //         'store_id' => $item['store_id'],
        //         'room_id' => $item['room_id'],
        //         'starttime' => $item['starttime'],
        //         'endtime' => $item['endtime'],
        //         'fee' => $item['fee'],
        //         'pay_way' => $item['pay_way'],
        //         're_status' => 1 == $item['is_refunded'] ? '2' : '1',
        //         'card_id' => $item['card_id'],
        //         'balance_id' => $item['balance_id'],
        //         'transaction_id' => $item['transaction_id'],
        //         'opendoor_pwd' => $item['opendoor_pwd'],
        //         'yunding_password_id' => $item['yunding_password_id'],
        //         'yunding_serviceid' => $item['yunding_serviceid'],
        //         'yunding_errno' => $item['yunding_errno'],
        //         'createtime' => $item['createtime'],
        //         'updatetime' => $item['updatetime'],
        //     ]);
        //     $count++;
        //     echo $count . PHP_EOL;
        // }
//
//        $data = Reserve::all();
//
//        $count = 0;
//        foreach ($data as $item) {
//            if (1 == $item['pay_way'] and 0 != $item['re_status']) {
//                $kk = Carbon::parse($item['starttime'])->diffInMinutes(Carbon::parse($item['endtime']));
////                echo $item['starttime'] . ', ' .$item['endtime'].PHP_EOL;
////                echo $kk . PHP_EOL;
////                echo $kk / 30 . PHP_EOL;
//                ReservePays::create([
//                    'reserve_id' => $item['id'],
//                    'user_id' => $item['user_id'],
//                    'pay_amount' => $kk / 30,
//                    'pay_way' => 1,
//                    'pay_id' => $item['card_id'],
//                    'pay_status' => 1 == $item['re_status'] ? '1' : '0',
//                    'createtime' => $item['createtime'],
//                    'updatetime' => $item['updatetime'],
//                ]);
//            }
//            $count++;
//            echo $count . PHP_EOL;
//        }

        $data = Reserve::all();

        $count = 0;
        foreach ($data as $item) {
            $minute = Carbon::parse($item->endtime)->minute;


            if (0 == $minute or 30 == $minute) {
//                echo $item->id .'===>'. $item->endtime . '====>' . Carbon::parse($item->endtime)->subMinute()->toDateTimeString(). PHP_EOL;
                $item->endtime = Carbon::parse($item->endtime)->subMinute()->toDateTimeString();
                $item->save();
            }

            $count++;
            echo $count . PHP_EOL;
        }

        $etime = microtime(true);//获取程序执行结束的时间
        $total = $etime - $stime;   //计算差值
        $output->writeln($total);
    }

}
